using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class AdvertisingController : Controller
    {
        //
        // GET: /Advertising/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        public ActionResult Index()
        {
            var rs = _db.QUANG_CAO;
            foreach (var qc in rs)
            {
                qc.NGUOI_DUNGReference.Load();
            }
            return View(_db.QUANG_CAO.ToList());
        }

        //
        // GET: /Advertising/Details/5

        public ActionResult Details(int id)
        {
            var rs = _db.QUANG_CAO.Where(qc => qc.id == id).First();
            rs.NGUOI_DUNGReference.Load();
            return View(rs);
        }

        //
        // GET: /Advertising/Create

        public ActionResult Create()
        {            
            ViewData["NGUOI_DUNG"] = new SelectList(_db.NGUOI_DUNG.ToList(), "id", "Ten");
            return View();
        } 

        //
        // POST: /Advertising/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]QUANG_CAO quangCaoToCreate, FormCollection formcollection)
        {
            try
            {
                // TODO: Add insert logic here
                int IdNguoiDung = int.Parse(formcollection.Get("IdNguoiDung"));
                quangCaoToCreate.NGUOI_DUNG = _db.NGUOI_DUNG.Where(nd => nd.id == IdNguoiDung).First();
                _db.AddToQUANG_CAO(quangCaoToCreate);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["NGUOI_DUNG"] = new SelectList(_db.NGUOI_DUNG.ToList(), "id", "Ten");
                ViewData["error"] = ex.Message;
                return View();
            }
        }

        //
        // GET: /Advertising/Edit/5
 
        public ActionResult Edit(int id)
        {
            var rs = _db.QUANG_CAO.Where((c) => c.id == id).FirstOrDefault();
            rs.NGUOI_DUNGReference.Load();
            ViewData["NGUOI_DUNG"] = new SelectList(_db.NGUOI_DUNG.ToList(), "id", "Ten", rs.NGUOI_DUNG.id);
            return View(rs);
        }

        //
        // POST: /Advertising/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(QUANG_CAO quangCaoToUpdate, FormCollection collection)
        {
            try
            {                
                var originalQuangCao = _db.QUANG_CAO.Where(qc => qc.id == quangCaoToUpdate.id).FirstOrDefault();
                //originalQuangCao.NGUOI_DUNGReference.Load();

                //TODO: kiem tra loi cho nay.
                ////kiem tra co thay doi nguoi dung ko.
                //int idNguoiDung = int.Parse(collection.Get("IdNguoiDung"));
                //if (idNguoiDung != originalQuangCao.NGUOI_DUNG.id)
                //    quangCaoToUpdate.NGUOI_DUNG = _db.NGUOI_DUNG.Where(nd => nd.id == idNguoiDung).First();
                
                if (!ModelState.IsValid)
                    return View(originalQuangCao);

                _db.ApplyPropertyChanges(originalQuangCao.EntityKey.EntitySetName, quangCaoToUpdate);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["NGUOI_DUNG"] = new SelectList(_db.NGUOI_DUNG.ToList(), "id", "Ten");
                ViewData["error"] = ex.Message;
                return View();
            }
        }        
    }
    public class QuangCaoManager
    {
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        public QUANG_CAO getCurrentQuangCao()
        {
            List<QUANG_CAO> qc = _db.QUANG_CAO.Where(q => q.NgayBatDau < DateTime.Now && q.NgayKetThuc > DateTime.Now).ToList();
            int num = qc.Count;
            if (num == 0)
                return null;
            Random r = new Random();
            int roll = r.Next(0, num - 1);
            return qc[roll];
        }
    }
}
